package com.bobo.base.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;

import com.bobo.base.R;
import com.bobo.base.widget.callback.CustomTitleCallback;

/**
 * 描述：
 * 作者: sheng
 * 日期: 2025/7/24 10:14
 */
public class CustomTitleLayout extends FrameLayout {

    private View layout;
    private ImageView ivTitleBack, ivTitleRight;
    private TextView tvTitleBack, tvTitleName;

    private String title;
    private String leftStr;
    private @ColorRes int titleColor = R.color.text_title;
    private @ColorRes int leftStrColor = R.color.text_title;
    private @DrawableRes int leftRes = 0;
    private @DrawableRes int rightRes = 0;
    private @DrawableRes int backgroundRes = 0;

    public CustomTitleLayout(Context context) {
        this(context, null);
    }

    public CustomTitleLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomTitleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public CustomTitleLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        layout = LayoutInflater.from(context).inflate(R.layout.layout_custom_title, this, true);
        initTitleView();
    }

    @SuppressLint("ResourceType")
    private void initTitleView() {
        ivTitleBack = layout.findViewById(R.id.ivTitleBack);
        ivTitleRight = layout.findViewById(R.id.ivTitleRight);
        tvTitleBack = layout.findViewById(R.id.tvTitleBack);
        tvTitleName = layout.findViewById(R.id.tvTitleName);

        setTitle(title);
        setTitleColor(titleColor);
        setLeftStr(leftStr);
        setLeftStrColor(leftStrColor);
        setLeftRes(leftRes);
        setRightRes(rightRes);
        setBackgroundRes(backgroundRes);

        ivTitleBack.setOnClickListener(v -> {
            if (leftCallback != null)
                leftCallback.onClick();
        });

        tvTitleBack.setOnClickListener(v -> {
            if (leftTextCallback != null)
                leftTextCallback.onClick();
        });

        ivTitleRight.setOnClickListener(v -> {
            if (rightCallback != null)
                rightCallback.onClick();
        });
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        this.title = title;
        tvTitleName.setText(title != null ? title : "");
    }

    /**
     * 设置标题颜色
     */
    @SuppressLint("ResourceType")
    public void setTitleColor(@ColorRes int titleColor) {
        this.titleColor = titleColor;
        tvTitleName.setTextColor(ContextCompat.getColor(getContext(), titleColor > 0 ? titleColor : R.color.text_title));
    }

    /**
     * 设置左边按钮颜色
     */
    @SuppressLint("ResourceType")
    public void setLeftStrColor(@ColorRes int leftStrColor) {
        this.leftStrColor = leftStrColor;
        tvTitleBack.setTextColor(ContextCompat.getColor(getContext(), leftStrColor > 0 ? leftStrColor : R.color.text_title));
    }

    /**
     * 设置返回按钮
     *
     * @param leftRes
     */
    public void setLeftRes(int leftRes) {
        this.leftRes = leftRes;

        if (leftRes > 0) {
            ivTitleBack.setImageResource(leftRes);
            ivTitleBack.setVisibility(View.VISIBLE);
        }

        setLeftTextPadding();
    }

    @SuppressLint("ResourceType")
    private void setLeftTextPadding() {
        if (tvTitleBack == null)
            return;

        if (leftRes > 0) {
//            tvTitleBack.setPadding(0, DpUtils.dp2px(5), 0, DpUtils.dp2px(5));
            tvTitleBack.setPadding(0, 15, 0, 15);
        } else {
//            tvTitleBack.setPadding(DpUtils.dp2px(15), DpUtils.dp2px(5), DpUtils.dp2px(15), DpUtils.dp2px(5));
            tvTitleBack.setPadding(45, 15, 45, 15);
        }
    }

    /**
     * 设置左边文字
     *
     * @param leftStr
     */
    public void setLeftStr(String leftStr) {
        this.leftStr = leftStr;

        if (!TextUtils.isEmpty(leftStr)) {
            tvTitleBack.setText(leftStr);
            tvTitleBack.setVisibility(View.VISIBLE);
        } else {
            tvTitleBack.setText("");
            tvTitleBack.setVisibility(View.GONE);
        }
    }

    /**
     * 设置右边按钮
     *
     * @param rightRes
     */
    public void setRightRes(int rightRes) {
        this.rightRes = rightRes;

        if (rightRes > 0) {
            ivTitleRight.setImageResource(rightRes);
            ivTitleRight.setVisibility(View.VISIBLE);
        } else {
            ivTitleRight.setImageResource(0);
            ivTitleRight.setVisibility(View.GONE);
        }
    }

    /**
     * 设置背景颜色
     *
     * @param backgroundRes
     */
    @SuppressLint("ResourceType")
    public void setBackgroundRes(@DrawableRes int backgroundRes) {
        this.backgroundRes = backgroundRes;

        setBackgroundResource(backgroundRes > 0 ? backgroundRes : R.color.app_color_white);
    }

    private CustomTitleCallback.CustomTitleLeftCallback leftCallback;

    private CustomTitleLayout setLeftCallback(CustomTitleCallback.CustomTitleLeftCallback leftCallback) {
        this.leftCallback = leftCallback;
        return this;
    }

    private CustomTitleCallback.CustomTitleLeftTextCallback leftTextCallback;

    private CustomTitleLayout setLeftTextCallback(CustomTitleCallback.CustomTitleLeftTextCallback leftTextCallback) {
        this.leftTextCallback = leftTextCallback;
        return this;
    }

    private CustomTitleCallback.CustomTitleRightCallback rightCallback;

    private CustomTitleLayout setRightCallback(CustomTitleCallback.CustomTitleRightCallback rightCallback) {
        this.rightCallback = rightCallback;
        return this;
    }

    public static class Builder {
        private CustomTitleLayout titleLayout;

        public Builder(Context context) {
            titleLayout = new CustomTitleLayout(context);
        }

        /**
         * 设置标题
         */
        public Builder setTitle(String title) {
            titleLayout.setTitle(title);
            return this;
        }

        /**
         * 设置左边文字
         *
         * @param leftStr 为空不显示
         */
        public Builder setLeftStr(String leftStr) {
            titleLayout.setLeftStr(leftStr);
            return this;
        }

        /**
         * 设置标题颜色
         */
        public Builder setTitleColor(@ColorRes int titleColor) {
            titleLayout.setTitleColor(titleColor);
            return this;
        }

        /**
         * 设置左边按钮颜色
         */
        public Builder setLeftStrColor(@ColorRes int leftStrColor) {
            titleLayout.setTitleColor(leftStrColor);
            return this;
        }

        /**
         * 设置返回按钮
         *
         * @param leftRes 小于等于0 不显示
         */
        public Builder setLeftRes(@DrawableRes int leftRes) {
            titleLayout.setLeftRes(leftRes);
            return this;
        }

        /**
         * 设置右边按钮
         *
         * @param rightRes 小于等于0 不显示
         */
        public Builder setRightRes(@DrawableRes int rightRes) {
            titleLayout.setRightRes(rightRes);
            return this;
        }

        /**
         * 设置背景
         *
         * @param backgroundRes 小于等于0 显示 白色
         */
        public Builder setBackgroundRes(@DrawableRes int backgroundRes) {
            titleLayout.setBackgroundRes(backgroundRes);
            return this;
        }

        /**
         * 点击返回按钮回调事件
         *
         * @param leftCallback
         * @return
         */
        public Builder setLeftCallback(CustomTitleCallback.CustomTitleLeftCallback leftCallback) {
            titleLayout.setLeftCallback(leftCallback);
            return this;
        }

        /**
         * 点击左边文字回调事件
         *
         * @param leftTextCallback
         * @return
         */
        private Builder setLeftTextCallback(CustomTitleCallback.CustomTitleLeftTextCallback leftTextCallback) {
            titleLayout.setLeftTextCallback(leftTextCallback);
            return this;
        }

        /**
         * 点击右边按钮回调事件
         *
         * @param rightCallback
         * @return
         */
        private Builder setRightCallback(CustomTitleCallback.CustomTitleRightCallback rightCallback) {
            titleLayout.setRightCallback(rightCallback);
            return this;
        }

        public CustomTitleLayout build() {
            return titleLayout;
        }

    }

}
